package com.project.dao.impl;

import com.project.bean.LinkBean;
import com.project.bean.UserBean;

import java.sql.*;
import java.time.LocalDate;

/**
 * DAO的父类，
 */
public class BaseDao {
//    链接对象
    protected Connection con ;
//    sql集合语句
    protected PreparedStatement ps;
//    结果集
    protected ResultSet res;

    /**
     * 建立链接
     */
    public void setConnection(){
        try {
            Class.forName("com.mysql.jdbc.Driver");
            con= DriverManager.getConnection("jdbc:mysql://localhost:12345/db?characterEncoding=utf-8","root","lovo");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 关闭流
     */
    public void closeConnection(){
        try {
        if (res!=null){
                res.close();
            }
        if(ps!=null){
            ps.close();
        }
        if (con!=null){
            con.close();
        }
        }catch (SQLException e) {
            e.printStackTrace();
        }

    }

    /**
     * 添加
     * @return
     */
    public LinkBean getUser(ResultSet res){
        LinkBean link = new LinkBean();
        try {
            String bir = res.getString("l_birthday");
            link.setBirthday(bir!=null?LocalDate.parse(bir):null);
            link.setName(res.getString("l_name"));
            link.setId(res.getInt("pk_linkId"));
            link.setPhone(res.getString("l_phone"));
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return link;
    }

}
